// pages/PersonalCenter/BaskInTheSun/BaskInTheSun.js
const Controller = require('../../controllers/controllers').controller
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'小程序名片',
    showPhoneIcon: false,
    template: {}, //海报图片生成配置
    userInfo: {},
    telephone: '', //手机号码
    saveImage: '',
    cavImg: 'https://tse1-mm.cn.bing.net/th/id/OIP.bNNaDh0yqigvzZYSCSne_AHaKP?pid=Api&rs=1',
    logo: 'https://youyang-cdn.mdshuzhi.com/upload/pic_logo@2x.png',
    QRCode: '', //二维码图片
    width: 600,
    height: 900,
    hasLenLeft: 240,
    isfirsttips:false,
    topbg:'',
    bottombg:'',
    tipstop:'',
    tipsbottom:'',
    bgImg1:'',
  },
  // 展示手机icon
  showPhoneIcon(e) {
    console.log('展示手机icon', e)
    let telephone = wx.getStorageSync('telephone') || ''
    let showPhoneIcon = !this.data.showPhoneIcon
    let that = this
    // showPhoneIcon false为选择显示手机号
    if (this.data.userInfo.nick.length > 6) {
      this.setData({
        hasLenLeft: 260
      })
    } else if (this.data.userInfo.nick.length <= 4) {
      this.setData({
        hasLenLeft: 210
      })
    }
    this.setData({
      showPhoneIcon,
      telephone: !this.data.showPhoneIcon ? ' |  ' + telephone : ''
    })
    if (this.timer) {
      clearTimeout(this.timer)
    }
    this.timer = setTimeout(() => {
      that.getDraw()
    }, 300)
  },
  // 保存图片
  saveImgBtn() {
    console.log('保存图片')
    wx.showLoading({
      title: '保存中',
      mask: true,
    })
    app.saveImg(this.data.saveImage)

  },
  // 绘制成功
  onImgOK(res) {
    console.log('绘制成功', res)
    wx.hideLoading()
    this.setData({
      saveImage: res.detail.path
    })
  },
  // 绘制失败
  onImgErr(err) {
    console.log('绘制失败', err)
    wx.showToast({
      title: err
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!wx.getStorageSync('logintoken')) {
      app.ToLogin()
    } else {
      // 1合辑 2栏目 3音频 4达人榜 5推荐听单，6历史听单，7我的收藏，8达人主页，9晒一晒，10小程序名片
      Controller.shareImg({
          type: 10,
          token: wx.getStorageSync('logintoken')
        })
        .then(res => {
          console.log(res)
          if (res.code == 0) {
            this.setData({
              cavImg: res.data.baseData,
              userInfo: wx.getStorageSync('userInfo')
            })
            this.makeShare();
          }
        })
    }
    if(app.globalData.firsttips3){
      this.firsttips()
    }
  },
  // 绘画海报
  getDraw() {
    wx.showLoading({
      title: '正在努力生成中',
    })
    this.setData({
      template: {
        background: this.data.bgImg1,
        width: this.data.width + 'rpx',
        height: this.data.height + 'rpx',
        views: [{
            type: 'text',
            text: this.data.userInfo.nick + this.data.telephone, //用户名
            css: {
              maxLines: '2',
              height: '40rpx',
              bottom: '240rpx',
              left: '100rpx',
              fontSize: '20rpx',
              color: "#A8ACBB",
            },
          }, {
            type: 'text',
            text: '来有氧，一起听吧', //来有氧，一起听吧
            css: {
              height: '40rpx',
              bottom: '200rpx',
              left: '100rpx',
              fontSize: '24rpx',
              color: "#323232",
            },
          }, {
            type: 'text',
            text: '有氧FM·音频分享',
            css: {
              left: "200rpx",
              bottom: '50rpx',
              fontSize: '22rpx',
              color: "#A8ACBB",
            },
          }
        ],
      }
    })
  },
  // 生成二维码
  /* 
    参数：
    path：二维码扫描后跳转的页面
    scene：参数  如 a=3
  */
  // makeShareImg() {
  //   Controller.makeShareImg({
  //       source: 1,
  //       path: 'pages/Index/Index',
  //       scene:1
  //     })
  //     .then(res => {
  //       console.log('生成二维码', res)
  //       if (res.code == 0) {
  //         this.setData({
  //           QRCode: res.data.baseData
  //         })
  //         this.getDraw()
  //       }
  //     })
  // },
  makeShare(options,type) {
    let path="pages/Index/Index"
    var options=this.data.options;
    let data={source: 1,path}
    data.scene=1
    data.type=type || 2
    data.itemImg=this.data.cavImg
    Controller.makeShare(data).then(res => {
        if (res.code == 0) {
          let bgImg = res.data.baseData
          this.setData({
            bgImg1:bgImg
          })
          this.getDraw()
        }
      })
  },
  // 手机授权
  bindgetphonenumber(e) {
    console.log('手机授权', e)
    app.updatePhoneNumber(e.detail)
      .then(res => {
        this.setData({
          telephone: res.data.baseData
        })
      })

  },
  path(){
    var options=this.data.options;
    var path="pages/Index/Index"
    if(options.type==1){
      path="pages/Exclusive/Exclusive?id="+options.audioId
    }
    if(options.type==2){
      path="pages/Album/Detail/Detail?id="+options.audioId
    }
    if(options.type==3){
      path="pages/Playing/Playing?id="+options.audioId
    }
    if(options.type==4){
      path="pages/Talent/Talent"
    }
    return path
  },
  getPageHeight(){
      let systemInfo = wx.getSystemInfoSync()
      let windowHeight = systemInfo.windowHeight
      if(systemInfo.model=="iPhone X"){
        windowHeight=windowHeight;
        this.setData({
          isIphoneX:true
        })
      }
      this.setData({
        windowHeight
      })
      this.tips()
  },
  tips(){
    let query = wx.createSelectorQuery()
    query.select('#chooseBox_showPhone').boundingClientRect((rect) => {
      if(rect){
        let top = rect.top;
        let bottom = rect.bottom;
        let height = rect.height;
        if(this.data.isIphoneX){
          top=top 
        }
        this.setData({
          tipstop: top + height + 10,
          tipsbottom:this.data.windowHeight - top + 10
        })
      }
    }).exec()
  },
  firsttips(){
    let that=this;
    that.setData({
      isfirsttips:true
    })
    app.globalData.firsttips3=false
    that.getPageHeight()
  },
  tipsclose(){
    this.setData({
      isfirsttips:false
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    console.log('分享按钮', e)
    // if (e.from == "button") {
      let obj = {
        title: '小程序名片',
        imageUrl: this.data.saveImage,
        path:'/pages/Index/Index',
        success: function (res) {
          console.log('点击页面内的分享按钮--onShareAppMessage分享成功', res)
        },
        fail(err) {
          console.log('点击页面内的分享按钮--onShareAppMessage-fail', err)
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    console.log('分享按钮', e)
    // if (e.from == "button") {
      let obj = {
        title: '小程序名片',
        imageUrl: this.data.saveImage,
        path:'/pages/Index/Index',
        success: function (res) {
          console.log('点击页面内的分享按钮--onShareAppMessage分享成功', res)
        },
        fail(err) {
          console.log('点击页面内的分享按钮--onShareAppMessage-fail', err)
        }
      }
      return obj
    // }
  }
  
})